package org.ainlolcat.ainscience.statistics;

import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.templates.Template;
import org.ainlolcat.ainscience.tools;
import org.apache.log4j.Logger;
import org.jdom.Element;

import java.util.Hashtable;
import java.util.List;

/**
 * @author ainlolcat
 * Date: 11/5/11
 */
public class StatisticsCollectorImpl implements StatisticsCollector {
    private static Logger log = Logger.getLogger(StatisticsCollectorImpl.class);
    Entity entity;
    Statistic statistic = new Statistic();

    public StatisticsCollectorImpl(Entity entity){
        this.entity = entity;
    }

    public Hashtable<String, String> collect() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Hashtable<String, String> answer = new Hashtable<String, String>();
        Element statisticsElement = entity.getTemplate().getEntityParseParams(entity.getType()).getChild("statistics");
        if (statisticsElement!=null){
            List<Element> statistics = statisticsElement.getChildren("statistic");
            for(Element stat:statistics){
                String statName = stat.getAttributeValue("name");
                String className = stat.getAttributeValue("class");
                String columnID = stat.getAttributeValue("column");
                try{
                    if (statName!=null&&className!=null&&columnID!=null){
                        if (stat.getChild("parameters")!=null){
                            answer.putAll(getStatistic(statName, className, tools.parseInteger(columnID), stat.getChild("parameters")));
                        }else{
                            answer.putAll(getStatistic(statName, className, tools.parseInteger(columnID)));
                        }
                    }
                }catch(Exception e){
                    log.error("Cannot get statistic " + statName +  " for entity " + entity.getName(), e);
                }
            }
        }
        statistic.addValues(answer);
        entity.setStatistic(statistic);
        return answer;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public StatisticHandler getHandler(String name) {
        return StatisticStorage.getInstance().getHandler(entity, name);
    }

    private Hashtable<String,String> getStatistic(String name, String className,Integer columnID) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class curentCollector = Class.forName(className);
        StatisticHandler handler = (StatisticHandler) curentCollector.newInstance();
        StatisticStorage.getInstance().registerHandler(entity, name, handler);
        statistic.setHandler(name, handler);
        return handler.getStatistic(entity,columnID);
    }

    private Hashtable<String,String> getStatistic(String name, String className,Integer columnID, Element params) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class curentCollector = Class.forName(className);
        StatisticHandler handler = (StatisticHandler) curentCollector.newInstance();
        StatisticStorage.getInstance().registerHandler(entity, name, handler);
        statistic.setHandler(name, handler);
        return handler.getStatistic(entity,columnID,params);
    }
}
